@page
@using CKY.AgentPlatform.Web.Pages.RateLimit
@model IndexModel
@{
    Layout = "~/Pages/Index.cshtml";
}
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@inject IHtmlLocalizer<AgentPlatformResource> L

<div class="container-fluid">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">限流管理</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            <div class="btn-group me-2">
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#cleanupModal">
                    <i class="fas fa-broom me-1"></i> 清理数据
                </button>
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#whitelistModal">
                    <i class="fas fa-shield-alt me-1"></i> 管理白名单
                </button>
            </div>
        </div>
    </div>

    <!-- 限流统计概览 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">限流统计概览</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3 class="text-primary">@(Model.Statistics?.TotalRequests.ToString("N0") ?? "0")</h3>
                                <p class="text-muted">总请求数</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3 class="text-danger">@(Model.Statistics?.BlockedRequests.ToString("N0") ?? "0")</h3>
                                <p class="text-muted">被阻止请求</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3 class="text-warning">@(Model.Statistics?.BlockRate.ToString("F1")% ?? "0%")</h3>
                                <p class="text-muted">阻止率</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3 class="text-info">@(Model.Statistics?.ActiveClients.ToString("N0") ?? "0")</h3>
                                <p class="text-muted">活跃客户端</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 活跃客户端 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">活跃客户端</h5>
                </div>
                <div class="card-body">
                    @if (Model.ActiveClients != null && Model.ActiveClients.Any())
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>客户端标识</th>
                                        <th>IP地址</th>
                                        <th>用户代理</th>
                                        <th>请求数</th>
                                        <th>最后请求时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var client in Model.ActiveClients)
                                    {
                                        <tr>
                                            <td><code>@client.ClientIdentifier</code></td>
                                            <td>@client.IpAddress</td>
                                            <td><small>@client.UserAgent</small></td>
                                            <td><span class="badge bg-info">@client.TotalRequests</span></td>
                                            <td>@client.LastRequest.ToString("yyyy-MM-dd HH:mm:ss")</td>
                                            <td>
                                                <form method="post" class="d-inline">
                                                    <input type="hidden" name="clientIdentifier" value="@client.ClientIdentifier" />
                                                    <button type="submit" asp-page-handler="ViewClientStatistics" class="btn btn-sm btn-info">
                                                        <i class="fas fa-chart-bar me-1"></i> 统计
                                                    </button>
                                                    <button type="submit" asp-page-handler="ResetClient" class="btn btn-sm btn-warning">
                                                        <i class="fas fa-redo me-1"></i> 重置
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="text-center text-muted">
                            <i class="fas fa-users fa-2x mb-2"></i>
                            <p>暂无活跃客户端</p>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 客户端详细统计 -->
    @if (Model.ClientStatistics != null)
    {
        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">客户端详细统计 - @Model.ClientStatistics.ClientIdentifier</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3">
                                <div class="text-center">
                                    <h5 class="text-primary">@Model.ClientStatistics.TotalRequests.ToString("N0")</h5>
                                    <p class="text-muted">总请求数</p>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="text-center">
                                    <h5 class="text-danger">@Model.ClientStatistics.BlockedRequests.ToString("N0")</h5>
                                    <p class="text-muted">被阻止请求</p>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="text-center">
                                    <h5 class="text-warning">@Model.ClientStatistics.BlockRate.ToString("F1")%</h5>
                                    <p class="text-muted">阻止率</p>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="text-center">
                                    <h5 class="text-info">@Model.ClientStatistics.CurrentLimit</h5>
                                    <p class="text-muted">当前限制</p>
                                </div>
                            </div>
                        </div>
                        
                        @if (Model.ClientStatistics.Endpoints != null && Model.ClientStatistics.Endpoints.Any())
                        {
                            <div class="mt-3">
                                <h6>访问的端点</h6>
                                <div class="d-flex flex-wrap">
                                    @foreach (var endpoint in Model.ClientStatistics.Endpoints)
                                    {
                                        <span class="badge bg-secondary me-1 mb-1">@endpoint</span>
                                    }
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
    }

    <!-- 端点统计 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">端点统计</h5>
                </div>
                <div class="card-body">
                    @if (Model.EndpointStatistics != null && Model.EndpointStatistics.Any())
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>端点</th>
                                        <th>请求数</th>
                                        <th>阻止数</th>
                                        <th>阻止率</th>
                                        <th>平均响应时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var endpoint in Model.EndpointStatistics)
                                    {
                                        <tr>
                                            <td><code>@endpoint.EndpointKey</code></td>
                                            <td><span class="badge bg-info">@endpoint.RequestCount</span></td>
                                            <td><span class="badge bg-danger">@endpoint.BlockedCount</span></td>
                                            <td>
                                                <div class="progress" style="height: 20px;">
                                                    <div class="progress-bar @(endpoint.BlockedCount > endpoint.RequestCount * 0.1 ? "bg-danger" : "bg-warning")" 
                                                         role="progressbar" style="width: @(endpoint.RequestCount > 0 ? (endpoint.BlockedCount * 100 / endpoint.RequestCount) : 0)%"
                                                         aria-valuenow="@(endpoint.RequestCount > 0 ? (endpoint.BlockedCount * 100 / endpoint.RequestCount) : 0)" 
                                                         aria-valuemin="0" aria-valuemax="100">
                                                        @(endpoint.RequestCount > 0 ? (endpoint.BlockedCount * 100 / endpoint.RequestCount).ToString("F1") : "0")%
                                                    </div>
                                                </div>
                                            </td>
                                            <td>@endpoint.AverageResponseTime.ToString("F2")ms</td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="text-center text-muted">
                            <i class="fas fa-chart-line fa-2x mb-2"></i>
                            <p>暂无端点统计数据</p>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 限流事件日志 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">限流事件日志</h5>
                </div>
                <div class="card-body">
                    <form method="post" class="mb-3">
                        <div class="row align-items-end">
                            <div class="col-md-3">
                                <label asp-for="EventStartDate" class="form-label">开始时间</label>
                                <input type="datetime-local" asp-for="EventStartDate" class="form-control" />
                            </div>
                            <div class="col-md-3">
                                <label asp-for="EventEndDate" class="form-label">结束时间</label>
                                <input type="datetime-local" asp-for="EventEndDate" class="form-control" />
                            </div>
                            <div class="col-md-6">
                                <button type="submit" asp-page-handler="SearchEvents" class="btn btn-primary">
                                    <i class="fas fa-search me-1"></i> 搜索
                                </button>
                            </div>
                        </div>
                    </form>
                    
                    @if (Model.RecentEvents != null && Model.RecentEvents.Any())
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>时间</th>
                                        <th>客户端</th>
                                        <th>端点</th>
                                        <th>操作</th>
                                        <th>原因</th>
                                        <th>IP地址</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var eventLog in Model.RecentEvents)
                                    {
                                        <tr>
                                            <td>@eventLog.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")</td>
                                            <td><code>@eventLog.ClientIdentifier</code></td>
                                            <td><span class="badge bg-secondary">@eventLog.EndpointKey</span></td>
                                            <td><span class="badge bg-warning">@eventLog.Action</span></td>
                                            <td><small>@eventLog.Reason</small></td>
                                            <td>@eventLog.IpAddress</td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="text-center text-muted">
                            <i class="fas fa-exclamation-triangle fa-2x mb-2"></i>
                            <p>暂无限流事件</p>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 清理数据模态框 -->
<abp-modal id="cleanupModal" title="清理过期数据">
    <p>确定要清理过期的统计数据吗？这将删除7天前的所有统计信息。</p>
    <form method="post">
        <abp-button type="submit" button-type="Primary" text="确认清理" 
                   icon="broom" form-action="Cleanup" />
    </form>
</abp-modal>

<!-- 管理白名单模态框 -->
<abp-modal id="whitelistModal" title="管理IP白名单" size="Large">
    <form method="post">
        <div class="mb-3">
            <label class="form-label">当前白名单</label>
            <div class="list-group">
                @if (Model.Config?.IPWhitelist != null)
                {
                    @foreach (var ip in Model.Config.IPWhitelist)
                    {
                        <div class="list-group-item d-flex justify-content-between align-items-center">
                            <span>@ip</span>
                            <form method="post" class="d-inline">
                                <input type="hidden" name="ipAddress" value="@ip" />
                                <button type="submit" asp-page-handler="RemoveFromWhitelist" class="btn btn-sm btn-danger">
                                    <i class="fas fa-trash me-1"></i> 删除
                                </button>
                            </form>
                        </div>
                    }
                }
            </div>
        </div>
        
        <div class="mb-3">
            <label asp-for="WhitelistRequest.IpAddress" class="form-label">添加IP地址</label>
            <input type="text" asp-for="WhitelistRequest.IpAddress" class="form-control" placeholder="输入IP地址，如 192.168.1.1" />
        </div>
        
        <abp-button type="submit" button-type="Primary" text="添加到白名单" 
                   icon="plus" form-action="AddToWhitelist" />
    </form>
</abp-modal>